package com.egd.imapp.contoller.activity.chat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.contoller.activity.contact.GroupListActivity;
import com.egd.imapp.utils.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.modules.chat.interfaces.OnChatLayoutListener;
import com.hyphenate.easeui.widget.EaseTitleBar;

public class GroupChatActivity extends AppCompatActivity {
    private EaseChatFragment easeChatFragment;
    private ImageView iv_group_chat;
    private EaseTitleBar mEaseTiTleBar;
    private EMGroup emGroup;
    private String conversationId;
    private int chatType;
    private String TAG = "TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_chat);

        initIntent();

        initView();

        initData();

        initListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: GroupChatActivity进入运行状态");

        // 注册接受到 群解散或退群的广播
        // 检查当前类型为群聊
        if (chatType == EaseConstant.CHATTYPE_GROUP){
            LocalBroadcastManager mLBM = LocalBroadcastManager.getInstance(GroupChatActivity.this);

            BroadcastReceiver ExitGroupReceiver = new BroadcastReceiver(){

                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.e(TAG, "onReceive: 收到 群解散或退群的广播 getStringExtra = "+ intent.getStringExtra(Constant.GROUP_ID) + " emgourp.getid = "+ emGroup.getGroupId() +  "emGroup.getOwner() = "+ emGroup.getOwner() );
                    if (emGroup.getGroupId().equals(intent.getStringExtra(Constant.GROUP_ID))){
                        Toast.makeText(GroupChatActivity.this, "结束当前群聊页面", Toast.LENGTH_SHORT).show();
                        // 结束当前页面
                        finish();
                    }
                }
            };

            mLBM.registerReceiver(ExitGroupReceiver, new IntentFilter(Constant.EXIT_GROUP));
        }
    }

    private void initIntent() {
        Intent intent = getIntent();
        chatType = intent.getIntExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);

        conversationId = intent.getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID);

        emGroup = EMClient.getInstance().groupManager().getGroup(conversationId);
    }

    private void initView() {
        mEaseTiTleBar = findViewById(R.id.title_bar_message);;
        iv_group_chat = findViewById(R.id.iv_group_chat);;

//        setTitleBarRight();
    }

    private void initData() {
        mEaseTiTleBar.setTitle(emGroup.getGroupName());

        // 创建会话的fragment
        easeChatFragment = new EaseChatFragment();

        Bundle bundle = new Bundle();
        bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
        bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, chatType);

        easeChatFragment.setArguments(bundle);


        Log.e(TAG, "initData: 替换聊天布局 emGroup = "+ emGroup.getGroupName()  );
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_chat_fragment, easeChatFragment, "chat").commit();


    }

//    private void setTitleBarRight() {
//        if(chatType == Constant.CHATTYPE_SINGLE) {
//            mEaseTiTleBar.setRightImageResource(R.drawable.chat_user_info);
//        }else {
//            mEaseTiTleBar.setRightImageResource(R.drawable.chat_group_info);
//        }
//    }

    private void initListener() {

        mEaseTiTleBar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                onBackPressed();
            }
        });

        iv_group_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GroupChatActivity.this, GroupDetailActivity.class);

                intent.putExtra(Constant.GROUP_ID, emGroup.getGroupId());

                startActivity(intent);

                Toast.makeText(GroupChatActivity.this, " 进入群详情页面", Toast.LENGTH_SHORT).show();
            }
        });
    }
}